package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ProvinceEntity;
import com.sgb.kjwl.view.ui.adapter.home.ChooseAreaAdapter3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.PinyinUtil;

import java.util.ArrayList;
import java.util.List;

public class AreaWindow3 extends BasePopWindow {
    private ChooseAreaAdapter3 areaAdapter;
    private RecyclerView recyclerView;
    private EditText editSearch;
    private TextView cancelView;
    private OnCallBack onCallBack;
    private List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList;

    public AreaWindow3(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_supplier_area3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        bgView = holder.getPop().findViewById(R.id.cover_layout);
        editSearch = holder.getPop().findViewById(R.id.edit_search);
        cancelView = holder.getPop().findViewById(R.id.cancel_view);
        if (areaAdapter == null) {
            areaAdapter = new ChooseAreaAdapter3(mContext, null);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(areaAdapter);
        areaAdapter.setOnCallBack(new ChooseAreaAdapter3.OnCallBack() {
            @Override
            public void select(ProvinceEntity<List<ProvinceEntity>> listProvinceEntity) {
                dissMiss();
                if (onCallBack != null) {
                    onCallBack.select(listProvinceEntity);
                }
            }
        });

        editSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    cancelView.setVisibility(View.VISIBLE);
                } else {
                   cancelView.setVisibility(View.GONE);
                    editSearch.setFocusable(true);
                    editSearch.setFocusableInTouchMode(true);
                 //   closeKeybord(editSearch);
                }
            }
        });

        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                searchFilter(provinceEntityList, s.toString());
            }
        });

        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editSearch.setText("");
                editSearch.setFocusable(false);
            }
        });
    }

    public void searchFilter(List<ProvinceEntity<List<ProvinceEntity>>> entityList, String condition) {
        if (entityList == null) {
            return;
        }
        List<ProvinceEntity<List<ProvinceEntity>>> filterList = new ArrayList<>();
        if (condition.equals("")) {
            if (onCallBack != null) {
                onCallBack.filter(entityList);
            }
            return;
        }
        for (ProvinceEntity<List<ProvinceEntity>> entity : entityList) {
            String name = entity.getAreaName();
            String pinyin = PinyinUtil.chineneToSpell(name);
            if (pinyin.contains(condition) || name.contains(condition)) {
                filterList.add(entity);
            }
        }
        if (onCallBack != null) {
            onCallBack.filter(filterList);
        }
    }

    public void setData(List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList, int select) {
        this.provinceEntityList = provinceEntityList;
        areaAdapter.setAllArea(provinceEntityList, select);
    }

    public void setFilterData(List<ProvinceEntity<List<ProvinceEntity>>> provinceEntityList, int select) {
        areaAdapter.setAllArea(provinceEntityList, select);
    }

    public void setCurrentArea(String area) {
        areaAdapter.setCurrentArea(area);
    }

    public interface OnCallBack {
        void select(ProvinceEntity<List<ProvinceEntity>> listProvinceEntity);
        void filter(List<ProvinceEntity<List<ProvinceEntity>>> entityList);
        void closeKeybord();
    }

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }
}
